import 'package:gold/base/view_model.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_utils.dart';
import 'package:path_provider/path_provider.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:39 2024/11/25

class SettingsViewmodel extends Viewmodel {
  /// 缓存
  int _cache = 0;
  int get cache => _cache;
  set cache(int value) {
    if (_cache != value) {
      _cache = value;
      notifyListeners();
    }
  }

  /// 加载缓存
  Future loadCache() async {
    final document = await getApplicationDocumentsDirectory();
    final temporary = await getTemporaryDirectory();
    int size = 0;
    final cacheResult = await getTotalSizeOfDirectory(document);
    size += cacheResult;
    final temporaryResult = await getTotalSizeOfDirectory(temporary);
    size += temporaryResult;
    cache = size;
  }

  /// 清楚缓存
  Future clearCache() async {
    if (_cache <= 0) {
      '您的应用已经很干净啦~'.toast();
    } else {
      final document = await getApplicationDocumentsDirectory();
      final temporary = await getTemporaryDirectory();
      if (document.existsSync() || temporary.existsSync()) {
        await deleteDirectory(document);
        await deleteDirectory(temporary);
      }
      final docu = await getTotalSizeOfDirectory(document);
      final temp = await getTotalSizeOfDirectory(temporary);
      cache = docu + temp;
      '缓存已清除'.toast();
    }
  }
}
